/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* SdProjectTeamTypeService
* SD项目任命-项目组分类
*
* 修改纪录
* 2021-12-08 版本：1.0 wangxinhua 创建。
* @version 版本：1.0
* @author 作者：wangxinhua
* 创建日期：2021-12-08
*/
package com.topscomm.gpm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.gpm.pojo.GpmProjectTeamEntity;
import com.topscomm.gpm.pojo.GpmProjectTeamTypeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.gpm.service.IGpmProjectTeamTypeService;
import com.topscomm.gpm.service.impl.auto.GpmProjectTeamTypeServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "gpmProjectTeamTypeService")
@Scope(value = "singleton")
public class GpmProjectTeamTypeService extends GpmProjectTeamTypeServiceAuto implements IGpmProjectTeamTypeService {
	@Autowired
	private GpmProjectTeamService gpmProjectTeamService;
	/**
	 * @Description:    插入团队类型
	 * @Author:         mengmeng
	 * @Date:           2021/8/9 16:32
	 * @Version:        1.0
	 */
	@Override
	@Transactional
	public GpmProjectTeamTypeEntity insertTeamType(String projectCode, Long appointmentId, Long projectid) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" {0} = '{1}' and {2} = {3}", GpmProjectTeamTypeEntity.FieldProjectcode, projectCode, GpmProjectTeamTypeEntity.FieldAppointmentid, appointmentId);
		int count = this.getCount(sql.toString());
		if (count > 0) {
			return null;
		}
		GpmProjectTeamTypeEntity entity = new GpmProjectTeamTypeEntity();
		entity.setAppointmentid(appointmentId);
		entity.setProjectcode(projectCode);
		entity.setProjectid(projectid);
		entity.setTeamname("项目核心组");
		return this.insert(entity);
	}

	/**
	 * @Description:    根据任命书id查询
	 * @Author:         mengmeng
	 * @Date:           2021/8/28 16:24
	 * @Version:        1.0
	 */
	@Override
	public List<GpmProjectTeamTypeEntity> queryByAppointmentId(String appointmentId) {
		if (StringUtil.isEmpty(appointmentId)) {
			return new ArrayList<>();
		}
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" appointmentid = '{0}'", appointmentId);
		sql.appendSingle(" order by createon desc ");
		return this.queryByWhere(sql.toString());
	}

	@Override
	protected void beforeInsert(BasicEntity entity) throws ServiceException {
		List<GpmProjectTeamTypeEntity> teamTypeList = queryByAppointmentId(ConvertUtil.convertToString(entity.getValue(GpmProjectTeamTypeEntity.FieldAppointmentid)));
		for (GpmProjectTeamTypeEntity teamTypeEntity : teamTypeList) {
			if (teamTypeEntity.getValue(GpmProjectTeamTypeEntity.FieldTeamname).equals(entity.getValue(GpmProjectTeamTypeEntity.FieldTeamname))) {
				throw new ServiceException("工作组名称不能重复！");
			}
		}
	}

	/***
	 * @description: 工作组名称修改后，修改对应成员名称。
	 * @param map
	 * @return
	 * @author: jiangtao1
	 * @date: 2022/6/7 18:15
	 * @modify:
	 */
	@Override protected void updateAfter(Map<String, Object> map) throws ServiceException {
		super.updateAfter(map);
		String projectcode = ConvertUtil.convertToString(map.get("projectcode"));
		String id = ConvertUtil.convertToString(map.get("id"));
		String teamname = ConvertUtil.convertToString(map.get("teamname"));
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("projectcode = '{0}' and teamid = '{1}'",projectcode,id);
		StringBufferProxy updateSql = new StringBufferProxy();
		updateSql.appendSingle("update {0} set workgroup = '{1}' where {2}", GpmProjectTeamEntity.tableName,teamname,sql.toString());
		gpmProjectTeamService.updateExecute(updateSql.toString());
	}
}
